home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12991 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.arch.embedded,comp.lang.c
  4. Subject: Re: Using malloc of C on embedded system?
  5. Date: 3 Apr 1996 13:00:25 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4juot9INNl69@anvil.ugrad.cs.ubc.ca>
  8. References: <4jml7h$cbc@castor.usc.edu> <4jrndq$ate@kuikka.inet.fi> <4jtbot$o1@Mars.mcs.com>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4jtbot$o1@Mars.mcs.com>, John Payson <supercat@MCS.COM> wrote:
  12.  >In article <4jrndq$ate@kuikka.inet.fi>,
  13.  >Risto Jokinen  <hatrjo@hat-fi.kone.com> wrote:
  14.  >>This mechanism works. random size malloc/free does not work on embedded systems, or
  15.  >>at least it is impossible to debug, and can be VERY slow. fixed size partition pool
  16.  >>manager takes <<50us for malloc/free in any processor.
  17.  >
  18.  >Fixed-size malloc/free is often a good approach if it works; to help it
  19.  >along, however, it's often useful to have a "version" of malloc for odd-
  20.  >sized blocks that will never be freed (allocate a large memory pool for
  21.  >these things and just track the last byte used), and sometimes useful to
  22.  >have several memory pools for different-sized objects.
  23.  >
  24.  >As I mentioned in another post, the "binary buddy" system is often a good
  25.  >compromise between internal and external fragmentation in embedded systems.
  26.  
  27. Also the fibonacci buddy system is also interesting. Whereas the binary buddy
  28. system is based on the recurrence t[n] = 2t[n-1], t[0] = 1, fibonacci is based
  29. on, of course, t[n] = t[n-1] + t[n-2], with t[0] = t[1] = 1.
  30.  
  31. Fibonacci will break the root cluster into two fibonacci numbers (the one which
  32. precede N in the fibonacci sequence). If the starting chunk is size 13, and 2
  33. units are requested, it will be broken into 8 and 5.  The smaller one will be
  34. recursively checked, and broken into 2 and 3. The size 2 chunk will then be
  35. assigned. If another request for size 2 is made, the 3 will be broken into 1
  36. and 2.
  37.  
  38.  >The system is somewhat confusing to describe, but it offers lg(N) performance
  39.  >without too much code or--if power-of-two memory sizes work well for your
  40.  >application--memory overhead.  It also tends to produce minimal fragmentation:
  41.  >while isolated unallocated spaces may get formed when non-consecutive blocks
  42.  >are freed, such spaces will have effective priority when allocating new blocks.
  43.  
  44. I would also consider  a system that has a means to do heap compaction. It's
  45. probably the only sure way to assure that the worst case fragmentation cannot
  46. happen. This introduces quite a bit of complexity, but may be worthwhile when
  47. memory is scarce. Plus one can always have escape hatches, such as the ability
  48. to nail an allocated chunk in place so that it can't be moved by the compactor.
  49.  
  50. -- 
  51.  
  52.